//查询营业员信息
var orderRow;
function querySalesCodes(row) {
    orderRow = row;
    var html = $.openx.innerHtml('salesCodesTpl');
    ajax(jot_posting_server + "/posting/postingQueryService", "querySalesCodes").callx({
        orderCode: row.orderCode,
        onResult: function (result) {
            layer.closeAll('loading');
            result.orderCode = row.orderCode;
            result.row = JSON.stringify(row).replace(/"/g, '&quot;');
            laytpl($.openx.innerHtml('salesCodesTpl')).render(result, function (html) {
                $.openx.openLayer(null, '打印小票', html, true);
            });
        },
        onError: function (error) {
            layer.msg(error.msg);
        }
    });
}

/**选择营业员*/
function submitSalesCode(orderCode) {
    var orderCodes =[];
    orderCodes.push(orderCode);
    var selectUserCode = $("#salesCodeSelect").val();
    if (selectUserCode === '') {
        layer.tips("营 业 员 不 能 为 空", '#salesCodeSelect');
        return ;
    }
    saveOperatorCode(orderCode,selectUserCode);
    layer.closeAll();
    createDelivery(orderRow, orderCode, orderCodes);
}

function saveOperatorCode(orderCode, operatorCode) {
    var postingModel = {};
    postingModel.orderCode = orderCode;
    postingModel.operatorCode = operatorCode;
    ajax(jot_posting_server + "/posting/postingService", "savePostingModel").callx({
        postingModel: postingModel,
        onResult: function (result) {
            console.info("保存下账数据成功");
        },
        //请求失败执行
        onError: function (error) {
            console.info(error);
            layer.msg("保存下账数据失败: " + error);
        }
    });
}

function createDelivery(row, orderCode, orderCodes) {
    // 1、判断是否是自提单
    if (row.stateCode === 'DELIVERY_WAITING' && row.deliveryType === 'SELF_PICK') {
        openTicketDialog(orderCode, orderCodes);
        return;
    }
    if (row.stateCode === 'DELIVERY_WAITING' && row.deliveryProvider) {//待发货并且是益丰配送则请求物流平台进行发货处理
        $.openx.confirm('打印小票，同时会进行发货操作。是否继续？', function () {
            layer.closeAll();
            //通知物流平台进行发货操作
            // $X("/order/operateService", "createOrderDeliveryTask").callx({
            //     orderCode: orderCodes[0],
            //     onResult: function (result) {
            //         if (result === 0) {
            //             $.openx.tableRefresh(null, null, true);
            //             openTicketDialog(orderCode, orderCodes)
            //         } else {
            //             layer.msg('发货失败，请联系管理员!');
            //         }
            //     },
            //     onError: function (error) {
            //         layer.msg(error.msg);
            //     }
            // });
            createTask(orderCode, orderCodes);
        });
    } else {
        openTicketDialog(orderCode, orderCodes)
    }
}

function getXhr() {
    var xhr = null;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject('Microsoft.XMLHTTP');
    }
    else {
        throw new Error('your browser not support http request');
    }
    return xhr;
}

function createTask(orderCode, orderCodes) {
    const xhr = this.getXhr();
    let url = ecp_boss_server + "/order/operateService/createOrderDeliveryTask";
    xhr.open('POST', url, false);
    xhr.setRequestHeader('Content-Type', 'application/json');
    try {
        var openx_header = getOpenxHeader();
        var data = {"orderCode":orderCodes[0], "_openx_head": openx_header};
        const configResult = xhr.send(JSON.stringify(data));
        console.info("返回值：", configResult);
        if (xhr.status === 200) {
            $.openx.tableRefresh(null, null, true);
            openTicketDialog(orderCode, orderCodes);
        } else if (xhr.status === 500) {
            const configResp = xhr.response;
            const configData = JSON.parse(configResp);
            layer.msg("订单发货失败" + configData.message);
        } else {
            layer.msg("订单发货失败!");
        }
    } catch (e) {
        console.info(e);
        layer.msg(e);
    }
}
